package leetcode.offer;

import leetcode.base.ListNode;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: CopyRandomList
 * Description:
 * date: 2021-10-28 20:42
 *
 * @author liyifan
 */
public class CopyRandomList {
    Map<Node, Node> cacheNode = new HashMap<>();
    public Node copyRandomList(Node head) {
        if (head == null)
            return null;

        if (!cacheNode.containsKey(head)) {
            Node newNode = new Node(head.val);
            cacheNode.put(head, newNode);
            newNode.next = copyRandomList(head.next);
            newNode.random = copyRandomList(head.random);
        }

        return cacheNode.get(head);
    }
}

class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}